package com.tuzhi.gateway.service;

import com.tuzhi.gateway.httpclient.face.AuthApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Slf4j
@Component
public class ResServiceImpl {


    static Set<String> notAuthUrl = new HashSet<>();
    @Autowired
    AuthApi authApi;

    /**
     * 获取不需要校验权限的地址集合,如果本地变量中有值，则直接取，如果没有值
     * 调用接口从redis中取
     *
     * @return
     */
    public Set<String> getNonAuthUrl() {
        if (!notAuthUrl.isEmpty()) {
            return notAuthUrl;
        }
        try {
            Set<String> notAuthUrls = authApi.getNonAuthUrl();
            if (notAuthUrls != null && !notAuthUrls.isEmpty()) {
                notAuthUrl.addAll(notAuthUrls);
                notAuthUrl.add("/bill/org/download/image/*");
                notAuthUrls.add("/bill/org/download/image/*");
                log.info("==调用接口加载不校验权限的集合地址==");
                notAuthUrls.forEach(item -> {
                    log.info(item);
                });
                log.info("==调用接口加载不校验权限的集合地址 以上内容==");
                return notAuthUrls;
            }
        } catch (Exception ex) {
            log.error("调用接口（authApi.getNonAuthUrl()）获取资源时出错，{}", ExceptionUtils.getStackTrace(ex));
        }

        //如果调用接口获取的值为空，则设置系统默认的配置
        /**
         if (notAuthUrls == null || notAuthUrls.isEmpty()) {
         log.info("网关加载默认不较验的地址 ");
         notAuthUrl.add("/auth/4app/*");
         notAuthUrl.add("/auth/logout");
         notAuthUrl.add("/bill/api/model/uploads/tuovisual/*");
         notAuthUrl.add("/moldbase/rulecheck/order/*");
         notAuthUrl.add("/auth/login");
         notAuthUrl.add("/auth/config/*");
         notAuthUrl.add("/bill/res/standard/pic/*");
         notAuthUrl.add("/nc/worktime*");
         notAuthUrl.add("/bill/res/standard/*");
         notAuthUrl.add("/moldbase/rulecheck/order/*");
         notAuthUrl.add("/auth/sys/user/personal/*");
         notAuthUrl.add("/auth/sys/user/modifyPwd");
         notAuthUrl.add("/fileStore/api/model/uploads/tuovisual/*");

         }**/
        log.info("加载不校验的记录为：{}",notAuthUrl!=null?notAuthUrl.size():0);
        return notAuthUrl;
    }

    public void clearNotAuthUrl() {
        log.info("====清除不校验权限的本地保存值====");
        notAuthUrl.clear();
    }
}
